package server.tests;


import java.io.Serializable;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.Vector;

import junit.framework.TestCase;

import server.comunicaciones.IServer;

@SuppressWarnings("serial")
public class TestServer extends TestCase implements IJugador, Serializable {
	private int port = 2995;
	private String ip="127.0.0.1";
	private Server localServer;
	private IServer remoteServer;
	private int id;
	private String ristra;

	//TestSUDOKU
	public void setUp() {
		try {
			this.localServer=new Server(null);
			this.localServer.conectar(Constantes.FILE_SYSTEM);
		} catch (Exception e) {
			fail("Unexpected exception");
		}
	}

	public void tearDown() {
		try {
			this.localServer.desconectar();
		} catch (Exception e) {
			fail("Unexpected exception: " + e.toString());
		}
	}
	//TestSUDOKU
	public void test1() {
		String url="rmi://" + ip + ":" + this.port + "/sudokuserver";
		try {
			this.remoteServer=(IServer) Naming.lookup(url);
			Vector<String> sudokus = this.remoteServer.getSudokus();
			int sizePre=sudokus.size();
			String sudoku3=this.localServer.getSudoku(this, "3", "pepe");
			assertEquals(sudoku3, "010000090528911036009020000800106004700000009900804001000040000180607023050000060");
			this.localServer.poner("3", (byte) 0, (byte) 0, (byte) 0, (byte) 1);
			Thread.sleep(500);
			assertTrue(this.id==3);
			assertEquals(this.ristra, "110000090528911036009020000800106004700000009900804001000040000180607023050000060");
			this.localServer.quitar("3", (byte) 0, (byte) 0, (byte) 0);
			Thread.sleep(500);
			assertTrue(this.id==3);
			assertEquals(sudoku3, "010000090528911036009020000800106004700000009900804001000040000180607023050000060");

			this.localServer.guardar("111111111111111111111111111111111111111111111111111111111111111111111111111111111");
			sudokus = this.remoteServer.getSudokus();
			assertTrue(sudokus.size()==sizePre+1);

		} catch (Exception e) {
			fail("Unexpected exception: " + e.toString());
		}
	}

	@Override
	public void actualizar(int id, String sudoku) throws RemoteException {
		this.id=id;
		this.ristra=sudoku;
	}
	
	@Override
	public String getNombre() {
		return "pepe";
	}
}
